package com.xl.utils.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.github.pagehelper.PageInfo;
import com.xl.utils.entity.TUser;
import com.xl.utils.model.qo.PageQO;
import com.xl.utils.model.vo.UserInfoVO;
import com.xl.utils.service.UserService;
import com.xl.utils.util.CodeImageUtil;
import com.xl.utils.util.ExcelUtil;
import com.xl.utils.util.OkHttpUtil;
import com.xl.utils.util.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;


/**
 * @Author xionglin
 * @Classname UserController
 * @Description TODO
 * @Date 2019/6/12 17:27
 */
@RequestMapping(value = "/user")
@RestController
@Api(value = "Swagger 2 控制层")
public class UserController {
    private UserService userService;
    private CodeImageUtil codeImageUtil;

    public UserController(UserService userService, CodeImageUtil codeImageUtil) {
        this.userService = userService;
        this.codeImageUtil = codeImageUtil;
    }


    @PostMapping(value = "/getUserInfo")
    public PageInfo getUser(PageQO pageQO, TUser user){
        //
        return userService.getUser(pageQO,user).toPageInfo();
    }


    @ApiOperation(value = "查找所有用户")
    @PostMapping(value = "/getUser")
    public PageInfo getUser(@ApiParam(value ="名字")String name){
        TUser tUser = new TUser();
        tUser.settName(name);
        return userService.getUser(tUser);
    }

    @GetMapping(value = "/findUser")
    public List<UserInfoVO> findUser(String name, Integer age){
        TUser tUser = new TUser();
        tUser.settName(name);
        tUser.settAge(age);
        final List<UserInfoVO> userByUser = userService.findUserByUser(tUser);
        return userByUser;
    }

    @PostMapping(value = "/findUserByName")
    public Object findUserByName(String name){
        TUser tUser = new TUser();
        tUser.settName(name);
        return  userService.findUserByUser(tUser);
    }

    @PostMapping(value = "/findUserByEntity")
    public Object findUserByEntity(@RequestBody TUser user){
        return  userService.findUserByUser(user);
    }

    @GetMapping(value = "/testOkHttp")
    public Object testOkHttp(String url){
        return OkHttpUtil.get(url, new HashMap<>());
    }

    /**
     * 测试阿里的easyexcel例子
     */
    @GetMapping(value = "/execlExport")
    public void execlExport(String name){
        OutputStream out = null;
        try {
             out = new FileOutputStream("D:\\My computer\\Desktop\\test.xlsx");
            //ExcelWriter writer = EasyExcelFactory.getWriter(out);
            ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLSX,true);
            // 写仅有一个 Sheet 的 Excel 文件, 此场景较为通用
            Sheet sheet1 = new Sheet(1, 0, UserInfoVO.class);
            sheet1.setSheetName("xl-test");
            TUser user = new TUser();
            user.settName(name);
            writer.write(userService.findUserByUser(user), sheet1);
            //单元合并
            //writer.merge(1,2,2,2);
            // 将上下文中的最终 outputStream 写入到指定文件中
            writer.finish();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            // 关闭流
            if(out != null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            }
        }
    }

    /**
     * 测试的上传excel用easyexcel解析
     */
    @PostMapping(value = "/excelImportTest2")
    public void excelImpor(@RequestParam("file")MultipartFile file){
            List<Object> objects = ExcelUtil.readMoreThan1000RowBySheet(file, null);
        System.out.println(objects);
    }

    /**
     * 测试阿里的easyexcel例子
     */
    @GetMapping(value = "/execlImportTest")
    public void execlImportTest(){
        List<Object> objects = ExcelUtil.readLessThan1000Row("D:\\My computer\\Desktop\\test.xlsx");
        System.out.println(objects);
    }

    /**
     * 生成验证码图片 session
     */
    @GetMapping(value = "/createCodeImage")
    public void testKaptchaCreateCodeImage(HttpServletRequest request, HttpServletResponse response){
        codeImageUtil.getKaptchaImage(response,request.getSession());
    }

    /**
     * 验证验证码图片 session
     */
    @GetMapping(value = "/verifyCodeImage")
    public boolean verifyKaptchaCreateCodeImage(HttpServletRequest request, String code){
        return codeImageUtil.verifyCapText(code,request.getSession());
    }

    /**
     * 生成验证码图片 redis
     */
    @GetMapping(value = "/createCodeImageRedis")
    public void testKaptchaCreateCodeImage(HttpServletResponse response ,String username){
        codeImageUtil.getKaptchaImage(response,username);
    }

    /**
     * 验证验证码图片 redis
     */
    @GetMapping(value = "/verifyCodeImageRedis")
    public boolean verifyKaptchaCreateCodeImage(String username, String code){
        return codeImageUtil.verifyCapText(code,username);
    }

    /**
     * 测试二维码
     * @param content
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/testQRCode")
    public void  testQRCode(String content,String imgPath,HttpServletRequest request,HttpServletResponse response) throws Exception {
        //http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg
        //D:/1.jpg
        //QRCodeUtil.encode(content,imgPath,"D:/123.jpg",true);
        QRCodeUtil.encode(content,imgPath,response.getOutputStream(),true);
    }

}
